﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Windows.Forms;

namespace BHair
{
    class chuanzhi
    {

        // 卡分类
        public static string yhfs;
        public static string jffs;
        public static int ifcz;
        public static int ifjc;
        public static decimal jfl;


        public static string xiugai_cardid = "";
        public static string tkcard;
        public static int gysid;
        public static int jsid;
        public static string jhid;
        public static string userID = "admin";
        public static int banb = 20170319;
        public static int isfo = 0;
        public static string cardID;
        public static decimal xssl = 0;
        public static int ifup = 0;
        public static int hyfl = 0;
        public static string hyflname = "";
        public static string hyzl = "";
        //public static int spID = 0;
        public static string banzu = "";
        public static string cardidpp = "";
        public static int ifpwd = 0;
        public static int xiuzeng = 0;
        public static decimal fkje = 0;
        public static decimal result = 0;
        public static string bkcardid = "";
        public static string xzkh = "";
        public static int cardfl = 0;
        public static string bankajine;
        public static string bankaname;
        public static string tianqi = "";
        public static string czcard = "";
        public static string tlcard = "";
        public static string tlname = "";
        public static string tzcardid = "";
        //商品分类
        public static int flid = 0;
        public static string flname = "";
        public static int flup = 0;
        public static int flde = 0;
        //销售
        public static decimal jzys = 0;//结账应收
        public static int jzbs = 0;//结账标识
        public static decimal knye = 0;//卡内余额
        public static decimal yezf = 0;//余额支付

        string constr = ConfigurationManager.ConnectionStrings["BHair.Properties.Settings.BHairDBConnectionString"].ConnectionString;
        /// <summary>
        /// 检查会员是否存在
        /// </summary>
        /// <param name="cardID"></param>
        /// <returns></returns>
        public bool CheckPass(string cardID)
        {
            string sql = string.Format("SELECT * FROM [member] where mid='{0}'", cardID);
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }

        }
        public bool checkxm(string cardid,string xmid)
        {
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql =string.Format( "SELECT *  FROM [csmx] where [cardID]='{0}' and [jcspid]='{1}'",cardid,xmid);
            SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                return true;
            }
            else { return false; }

        }
        public bool checktianqi(string date)
        {
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql = string.Format("SELECT * FROM [天气] where 日期='{0}'",date);
            SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            conn.Close();
            if (dt.Rows.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
            
        }
        /// <summary>
        /// 检查有效期
        /// </summary>
        /// <param name="cardID"></param>
        /// <returns></returns>
        public bool Checkyxq(string cardID)
        {
            DateTime riqi = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
            int ifyxq = 0;
            DateTime yxq = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
            //查询会员卡是否到期
            string sql99 = string.Format("SELECT [ifyxq] ,[yxq] FROM [member] where mid='{0}'", cardID);

            SqlConnection conn99 = new SqlConnection(constr);
            conn99.Open();
            SqlCommand comm99 = new SqlCommand(sql99, conn99);

            SqlDataReader sdr = comm99.ExecuteReader();
            while (sdr.Read())
            {
                ifyxq = int.Parse(sdr["ifyxq"].ToString());
                yxq = Convert.ToDateTime(sdr["yxq"].ToString());
            }
            if (ifyxq == 0)
            {
                return true;
            }
            else if (ifyxq == 1 & yxq < riqi)
            {
                return false;
            }
            else if (ifyxq == 1 & yxq >= riqi)
            {
                return true;
            }
            return false;
        }
        public bool checkpwd(string pwd)
        {
            string sql =string.Format( "SELECT * FROM [LoginUser] where LoginUserID='{0}' and LoginPassword='{1}'",chuanzhi.userID,pwd);
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            conn.Close();
            if (dt.Rows.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
            
        }
        /// <summary>
        /// 检查会员密码
        /// </summary>
        /// <param name="pwd"></param>
        /// <param name="cardID"></param>
        /// <returns></returns>
        public bool checkcardpwd(string pwd)
        {
            string sql =string.Format("SELECT * FROM [member] where mid='{0}' and mpass='{1}'",cardidpp,pwd);
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            conn.Close();
            if (dt.Rows.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool checkpwdifnull(string card)
        {
            try
            {
                string pwd = "";
                string sql = string.Format("SELECT [mpass] FROM [member] where mid='{0}'", card);
                SqlConnection conn = new SqlConnection(constr);
                conn.Open();
                DataTable dt = new DataTable();                               //假设字段为name, dt已经保存了数据

                SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
                sda.Fill(dt);
                //dt.rows[0]["name"] == System.DBNull.Value;
                if (dt.Rows[0]["mpass"].ToString() == string.Empty)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch
            {
                MessageBox.Show("请输入正确的卡号");
                return false;
            }
                
            
           
        }
        /// <summary>
        /// 商品是否打折
        /// </summary>
        /// <returns></returns>
        public bool shifoudazhe(string spid)
        {
            int iff = 0;
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql = string.Format("SELECT [isbonus] FROM [items] where iid={0}",spid);
            SqlCommand comm = new SqlCommand(sql,conn);
            SqlDataReader sdr = comm.ExecuteReader();
            while (sdr.Read())
            {
                iff =Int32.Parse( sdr["isbonus"].ToString());
            }
            conn.Close();
            if (iff == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
            
            
        }
            public void rizhi(string neirong)
            {
            string sql =string.Format( "INSERT INTO [操作日志]([操作员] ,[操作内容],[操作时间]) VALUES ('{0}' ,'{1}' ,'{2}')",userID,neirong, DateTime.Now);
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            SqlCommand comm = new SqlCommand(sql,conn);
            comm.ExecuteNonQuery();
            conn.Close();
            
            }
        public bool keyong(string cardID)
        {
            int iff = 0;
            string sql = string.Format("SELECT * FROM [member] where mid='{0}'", cardID);
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            SqlCommand comm = new SqlCommand(sql, conn);
            SqlDataReader sdr = comm.ExecuteReader();
            while (sdr.Read())
            {
                iff =Int32.Parse( sdr["mstatus"].ToString());
            }
            conn.Close();
            if (iff == 0)
            {
                return true;
            }
            else
            {
                return false;
            }


            
        }
    }
}
